﻿Imports System.Runtime.Serialization.Formatters.Binary
Imports OpenArchive.Document.Components.Data

Namespace OpenArchive
    Namespace Formatters.Binary
        Public Class InfoComponentBinaryFormatter
            Inherits BinaryComponentFormatter
            Sub New()

            End Sub
            Public Overrides Sub Serialize(ByVal serializationStream As Global.System.IO.Stream, ByVal Datacomponent As ComponentData)
                Dim _Infocompdata As InfoComponentData = TryCast(Datacomponent, InfoComponentData)
                If _Infocompdata Is Nothing Then Throw New Global.System.Runtime.Serialization.SerializationException()
                Serialize(serializationStream, _Infocompdata)
            End Sub
            Public Overloads Sub Serialize(ByVal serializationStream As Global.System.IO.Stream, ByVal Datacomponent As InfoComponentData)
                Try
                    Writer = New IO.ArchiveBinaryWriter(serializationStream, New Text.UTF8Encoding)
                    MyBase.Serialize(serializationStream, Datacomponent)
                    Writer.Write(Datacomponent.Name)
                    'Writer.Write(Datacomponent.ExternalID) ' It can't be searched
                    Writer.Write(Datacomponent.Created.ToBinary)
                    Writer.Write(Datacomponent.Guid.ToByteArray)
                    Writer.Write(Datacomponent.Retention.ToBinary)
                    Writer.Write(Datacomponent.DocType)
                    If Not Datacomponent.OtherProperties Is Nothing Then
                        Writer.Write(Datacomponent.OtherProperties.OuterXml)
                    Else
                        Writer.Write("")
                    End If
                    Writer = Nothing
                Catch ex As Exception
                    Writer = Nothing
                    Throw New Global.System.Runtime.Serialization.SerializationException(ex.Message)
                End Try
            End Sub
            Public Overrides Function Deserialize(ByVal serializationStream As Global.System.IO.Stream) As ComponentData

                Dim _infocompdata As InfoComponentData = New InfoComponentData()
                Reader = New IO.ArchiveBinaryReader(serializationStream, New Text.UTF8Encoding)
                Reader.ReadString()
                Reader.ReadVersion()
                ' The current position in the underlaying stream have to be after reading Type.Name and Version. These data had to be already read in the responibility check.
                _infocompdata.Name = Reader.ReadString
                _infocompdata.Created = Date.FromBinary(Reader.ReadInt64)
                _infocompdata.Guid = New Guid(Reader.ReadBytes(16))
                _infocompdata.Retention = Date.FromBinary(Reader.ReadInt64)
                _infocompdata.DocType = Reader.ReadString
                Dim _xmlstr As String = Reader.ReadString
                If Not String.IsNullOrEmpty(_xmlstr) Then
                    Dim _xmldoc As Xml.XmlDocument = New Xml.XmlDocument()
                    _xmldoc.LoadXml(_xmlstr)
                    _infocompdata.OtherProperties = _xmldoc
                End If
                Return _infocompdata


            End Function

            Public Overrides Function IsResponsible(ByVal Type As Global.System.Type, ByVal Version As Global.System.Version) As Boolean
                Return MyBase.CheckResponsibility(New InfoComponentData(), Type, Version)
            End Function
        End Class
    End Namespace
End Namespace

